package com.tang.Web;

import com.tang.pojo.User;
import com.tang.service.UserServiceImpl;
import com.tang.Utils.WebUtils;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

public class ActionServlet extends BaseServlet {

    public void login(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{
        req.setCharacterEncoding("UTF-8");
        UserServiceImpl usi=new UserServiceImpl();
        String name=req.getParameter("user");
        String password=req.getParameter("passwd");
        if(usi.login(name,password)){
            HttpSession session=req.getSession();
            session.setAttribute("userName",name);
            //req.getRequestDispatcher("pages/Welcome.jsp").forward(req,resp);
            resp.sendRedirect("pages/Welcome.jsp");
        }else{
            String str="用户名或密码错误";
            req.setAttribute("info",str);
            req.getRequestDispatcher("pages/login.jsp").forward(req,resp);
        }
    }
    public void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        req.setCharacterEncoding("UTF-8");
        String yanzheng=req.getParameter("yanzhengma");
        String key=(String)req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        req.getSession().removeAttribute(KAPTCHA_SESSION_KEY);
        if(!yanzheng.equalsIgnoreCase(key)){
            System.out.println("验证码不正确");
            String str2="验证码不正确";
            req.setAttribute("info2",str2);
            req.getRequestDispatcher("pages/register.jsp").forward(req,resp);
        }else{
            UserServiceImpl usi=new UserServiceImpl();
            User user=WebUtils.copyParam(req.getParameterMap(),new User());
            System.out.println("注入后:"+user);
            //user.setName(name);
            //user.setPassword(password);
           // user.setEmail(email);
            if(usi.queryIsExists(user.getName())){
                System.out.println("用户名已被注册");
                req.setAttribute("info","用户名已被注册");
                req.getRequestDispatcher("pages/register.jsp").forward(req,resp);
            }else {
                usi.register(user);
                req.getRequestDispatcher("pages/login.jsp").forward(req,resp);
            }
        }
    }
    public void loginOut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        req.getSession().invalidate();
        resp.sendRedirect("pages/Welcome.jsp");
    }
    public void confirmUserName(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{
        String name=req.getParameter("name");
        UserServiceImpl usi=new UserServiceImpl();
        if(usi.queryIsExists(name)){
            resp.getWriter().write("该用户名已存在");
        }else{
            //resp.getWriter().write("该用户名可用");
        }
    }




}

